package com.jelly.web.modules.staff.user.controller;

import com.jelly.common.constant.GlobalConstant;
import com.jelly.common.excel.ReportExcel;
import com.jelly.common.util.ListUtil;
import com.jelly.common.util.StringUtils;
import com.jelly.common.util.web.AjaxPostResponse;
import com.jelly.common.util.web.BuildTreeUtil;
import com.jelly.common.web.page.LayuiTableInfo;
import com.jelly.common.web.tree.Tree;
import com.jelly.common.note.OperationLog;
import com.jelly.web.share.base.controller.BaseController;
import com.jelly.web.share.base.domain.Base;
import com.jelly.web.modules.system.dept.domain.Dept;
import com.jelly.web.modules.system.dept.service.IDeptService;
import com.jelly.web.modules.system.role.service.IRoleService;
import com.jelly.web.modules.staff.user.domain.User;
import com.jelly.web.modules.staff.user.domain.UserAndRoleAndPermission;
import com.jelly.web.modules.staff.user.service.IUserService;
import org.apache.shiro.session.mgt.SessionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @创建人 ql
 * @创建时间 2018-08-20 09:32
 * @项目名称 jelly
 * @功能描述: 用户模块相关控制器
 *           1丶获取用户列表
 *           2丶新增用户信息
 *           3丶修改用户信息
 *           4丶删除用户信息
 *           5丶批量删除用户信息
 *           6丶导出
 **/
@Controller
@RequestMapping("/admin/staff/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDeptService deptService;
    @Autowired
    private SessionManager sessionManager;
    /**
     * 用户列表跳转
     * @return
     */
    @GetMapping
    public String getUserList(){

        //获取字典详情
        getDictionary();

        //获取该用户可访问的按钮详情
        getButton();

        return adminUser("/list");

    }

    /**
     * 用户列表详情
     * @param user
     * @return
     */
    @PostMapping
    @ResponseBody
    public LayuiTableInfo getUserList(User user){

        getPageHelper();

        return getPageHelper(userService.getUser(user));

    }

    /**
     * 用户详情跳转
     * @return
     */
    @GetMapping("/user")
    public String getUser(Model model,Base base){
        //获取字典详情
        getDictionary();

        model.addAttribute("user",userService.getUserRoleDept(base.getBaseId(),false));

        return adminUser("/user");

    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PostMapping("/update")
    @OperationLog(name = "用户模块",value = GlobalConstant.UPDATE)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> updateUser(User user){

        if(userService.updateUser(user)){

            return okResponse("修改成功");

        }

        return errorResponse("修改失败");

    }

    /**
     * 修改用户跳转
     * @return
     */
    @GetMapping("/update")
    public String getUserEdit(Model model,Base base){
        //获取字典详情
        getDictionary();
        UserAndRoleAndPermission userAndRoleAndPermission = new UserAndRoleAndPermission();
        if(StringUtils.isNotBlank(base.getBaseId())){
            userAndRoleAndPermission = userService.getUserRoleDept(base.getBaseId(),true);
        }

        model.addAttribute("user",userAndRoleAndPermission);

        //获取部门详情
        Tree<Dept> deptTree = deptService.getTreeDept();
        if(null != deptTree && null != userAndRoleAndPermission){
            //获取修改用户的部门详情
            model.addAttribute("userdept",BuildTreeUtil.builds(deptTree.getChildren(),userAndRoleAndPermission.getDeptId()));
            //获取所有部门详情（剔除最顶级部门）
            model.addAttribute("dept",deptTree.getChildren());
        }

        return adminUser("/update");

    }

    /**
     * 新增用户跳转
     * @return
     */
    @GetMapping("/add")
    public String addUser(Model model){
        //获取字典详情
        getDictionary();
        //获取部门详情
        model.addAttribute("dept",deptService.getTreeDept().getChildren());
        //获取所有角色
        model.addAttribute("role",roleService.getTreeRole());

        return adminUser("/add");

    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping("/add")
    @OperationLog(name = "用户模块",value = GlobalConstant.ADDED)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> addUser(User user){

        String loginAccount = user.getLoginAccount();

        if(StringUtils.isNotBlank(loginAccount)){

            if(null !=  userService.getLoginAccount(loginAccount)){

                return errorResponse("用户名重复");

            }else{

                if(userService.addUser(user)){

                    return okResponse("新增成功");

                }

            }

        }

        return errorResponse("新增失败");

    }

    /**
     * 删除用户
     * @param base
     * @return
     */
    @PostMapping("/delect")
    @OperationLog(name = "用户模块",value = GlobalConstant.DELECT)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectUser(Base base){

        if(StringUtils.isNotBlank(base.getBaseId())){

            if(userService.delectUser(base.getBaseId())){

                return okResponse("删除成功");

            }

        }

        return errorResponse("删除失败");

    }

    /**
     * 批量删除用户
     * @param base
     * @return
     */
    @PostMapping("/delects")
    @OperationLog(name = "用户模块",value = GlobalConstant.DELECTS)
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> delectsUser(Base base){

        if(ListUtil.isNotList(base.getIds())){

            if(userService.delectsUser(base.getIds())){

                return okResponse("删除成功");

            }

        }

        return errorResponse("删除失败");

    }
    /**
     * 导出用户
     * @param user
     * @return
     */
    @GetMapping("/export")
    @OperationLog(name = "用户模块",value = GlobalConstant.EXPORT)
    public void export(HttpServletRequest req, HttpServletResponse resp, User user){
        List<User> list = userService.getUser(user);
        ReportExcel reportExcel = new ReportExcel();
        try {
            reportExcel.excelExport(list,"用户Excel",User.class,1,resp,req);
        }catch (Exception e){
        }
    }


    /**
     * 用户信息验证
     * @param user
     * @return
     */
    @PostMapping("/verify")
    @ResponseBody
    public ResponseEntity<AjaxPostResponse> userVerify(User user){

        if(StringUtils.isNotBlank(user.getLoginAccount()) || StringUtils.isNotBlank(user.getMobile())){
            List<User> list = userService.getUser(user);
            if(ListUtil.isNotList(list)){
                if(StringUtils.isNotBlank(user.getLoginAccount())){
                    return errorResponse("用户名已注册");
                }else {
                    return errorResponse("手机号已注册");
                }
            }
        }
        return okResponse("");

    }

}
